package com.example.Gui;

import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;


public class LoginUI {


     public void init() throws Exception {

        FlatLightLaf.install();
        UIManager.setLookAndFeel(new FlatLightLaf());
         JFrame jFrame=new JFrame("用户登录界面");
         jFrame.setResizable(false);
         JPanel jp=new JPanel();
         JButton b1=new JButton("登录");
        // JButton b2=new JButton("注册");
         JLabel userLable=new JLabel("账号");
         TextField user=new TextField(15);
         JLabel passwdLable=new JLabel("密码");
         TextField passwd=new TextField(15);
         passwd.setEchoChar('*');
         Box boxues=Box.createHorizontalBox();
         Box boxpass=Box.createHorizontalBox();
         Box boxButton =Box.createHorizontalBox();
         Box boxLogin=Box.createVerticalBox();
         //将文本框和按钮都装入一个盒子中
         boxues.add(userLable);
         boxues.add(Box.createHorizontalStrut(15));
         boxues.add(user);
         boxpass.add(passwdLable);
         boxpass.add(Box.createHorizontalStrut(15));
         boxpass.add(passwd);
         boxButton.add(b1);
      //   boxButton.add(Box.createHorizontalStrut(50));
 //        boxButton.add(b2);
         boxLogin.add(Box.createVerticalStrut(70));
         boxLogin.add(boxues);
         boxLogin.add(Box.createVerticalStrut(10));
         boxLogin.add(boxpass);
         boxLogin.add(Box.createVerticalStrut(30));
         boxLogin.add(boxButton);
         //添加到面板中
         jp.add(boxLogin);
         jFrame.add(jp);
        jFrame.setVisible(true);
         //居中处理
         jFrame.setSize(400,300);
         jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

         //监听b1 登录
         b1.addMouseListener(new MouseAdapter() {
             @Override
             public void mouseClicked(MouseEvent e) {
                 Map<String,String> map=new HashMap<>();
                 map.put("id",user.getText());
                 map.put("passwd",passwd.getText());
                 String result=HttpUtil.sendPost("http://localhost:8080/login", map);
                 String s = ResultUtil.ResultLoginParse(result);
                 //权限为0
                 if(s.equals("0")){
                     //进入管理员系统界面
                     JOptionPane.showMessageDialog(null,"登录成功！" );
                     new AdminUi().init(user.getText(),passwd.getText());
                     jFrame.dispose();
                 }else if (s.equals("1")){
                     //进入老师系统界面
                     JOptionPane.showMessageDialog(null,"登录成功！" );
                      new TeacherUi().init(user.getText(),passwd.getText());
                      jFrame.dispose();
                 }else if(s.equals("2")){
                     //进入学生管理界面
                     JOptionPane.showMessageDialog(null,"登录成功！" );
                     new StudentUi().init(user.getText(),passwd.getText());
                     jFrame.dispose();
                 }else {
                     //登录错误 弹窗
                     JOptionPane.showMessageDialog(null, "用户名或密码错误！");
                 }
             }
         });

         //监听b2 注册
    /*   b2.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 //进入注册页面
                 new RegisterUi().reInit();
                 jFrame.dispose();
             }
         });*/

    }

    private void initialize() {
        FlatLightLaf.install();

        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());
        } catch (Exception ex) {
            System.err.println("Failed to initialize LaF");
        }


    }



}
